package com.ycx.ycxmybatismanagement.controller;

import com.ycx.ycxmybatismanagement.config.AliyunOssConfig;
import com.ycx.ycxmybatismanagement.pojo.Result;
import com.ycx.ycxmybatismanagement.util.AliyunOSSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
public class UploadController {
    @Autowired
    private AliyunOssConfig aliyunOssConfig;
//    @Value("${aliyun.oss.endpoint}")
//    private String endPoint ;
//    @Value("${aliyun.oss.bucket}")
//    private String bucket ;
//    private String accesskey = "LTAI5t8dRAKnB6YMjDLiUuph";
//    private String secrekey = "3uJiQbViZpgWkGOx4dnIFNmlwsuJuB";

//    @PostMapping("/upload")
    public Result<String> upload(@RequestParam("file") MultipartFile file) throws Exception {
        String originalFilename = file.getOriginalFilename();
        //后缀

        String extName = originalFilename.substring(originalFilename.lastIndexOf("."));
        //上传文件的字节数组
        byte[] content = file.getBytes();
        //调用util上传到aliyun oss
        String url = AliyunOSSUtils.upload(aliyunOssConfig.getEndpoint(), aliyunOssConfig.getBucket(), content, extName);
        //返回URL地址
        return Result.success(url);
    }









//    @PostMapping("/upload")
//    public String upload(String username,
//                         Integer age,
//                         MultipartFile file) throws Exception{
//        //获取原始文件名
//        String originalFilename = file.getOriginalFilename();
//        ////用UUID拼接文件
//        String newFileName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
//        //创建目标文件对象
//        File dst = new File("d:/", newFileName);
//        //将文件转移
//        file.transferTo(dst);
//        return "success";
//
//    }
//    @GetMapping("download")
//    public void download(String filename, HttpServletResponse response) throws Exception{
//        //设置相应数据的类型
//        response.setContentType("image/png");
//        //构造要下载的文件
//        File dst = new File("d:/", filename);
//        //获取文件输入流
//        FileInputStream fileInputStream = new FileInputStream(dst);
//        //响应输出流
//        ServletOutputStream outputStream = response.getOutputStream();
//        //复制流
//        IOUtils.copyLarge(fileInputStream,outputStream);
//        outputStream.close();
//        fileInputStream.close();
//    }




}
